package com.sanpu.kxe.rxservice.client;

import android.support.v4.util.Pair;
import android.text.TextUtils;

import com.alibaba.fastjson.JSONObject;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sanpu.kxe.business.login.bean.LoginOutput;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.UrlConstants;
import com.sanpu.kxe.rxservice.BaseClient;
import com.sanpu.kxe.rxservice.MetaBean;
import com.sanpu.kxe.rxservice.RequestMap;
import com.sanpu.kxe.rxservice.interfaces.LoginService;

import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;
import rx.functions.Func1;

/**
 * Description：登录模块接口
 * Created by 薛勇军 on 2018/6/14.
 */

public class LoginClient extends BaseClient {

    private static LoginClient mInstance;

    public static LoginClient getInstance() {
        if (mInstance == null) {
            synchronized (LoginClient.class) {
                if (mInstance == null) {
                    mInstance = new LoginClient();
                }
            }
        }
        return mInstance;
    }

    private LoginService service;

    private LoginClient() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(UrlConstants.getBaseUrl())
                .client(client)
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        service = retrofit.create(LoginService.class);
    }

    /**
     * 登录接口
     * @param reqData 请求参数
     */
    public Observable<LoginOutput> login(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapBeforeLogin(service.login(requestMap.getReqParams(false)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<LoginOutput>>() {
                    @Override
                    public Observable<LoginOutput> call(Pair<MetaBean, String> pair) {
                        if (pair == null) {
                            return toObservable(null);
                        }
                        LoginOutput output = null;
                        if (!TextUtils.isEmpty(pair.second)) {
                            output = new Gson().fromJson(pair.second, new TypeToken<LoginOutput>() {
                            }.getType());
                        }
                        return toObservable(new Pair<>(pair.first, output));
                    }
                });
    }

    /**
     * 注册接口
     * @param reqData 请求参数
     */
    public Observable<String> register(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapBeforeLogin(service.register(requestMap.getReqParams(false)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<String>>() {
                    @Override
                    public Observable<String> call(Pair<MetaBean, String> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 发送验证码
     * @param reqData 请求参数
     */
    public Observable<String> sendSms(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapBeforeLogin(service.sendSms(requestMap.getReqParams(false)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<String>>() {
                    @Override
                    public Observable<String> call(Pair<MetaBean, String> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 忘记密码
     * @param reqData 请求参数
     */
    public Observable<String> forgetPassword(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapBeforeLogin(service.forgetPassword(requestMap.getReqParams(false)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<String>>() {
                    @Override
                    public Observable<String> call(Pair<MetaBean, String> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 修改密码
     * @param reqData 请求参数
     */
    public Observable<String> updateLoginPwd(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.updateLoginPwd(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<String>>() {
                    @Override
                    public Observable<String> call(Pair<MetaBean, String> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 修改支付密码
     * @param reqData 请求参数
     */
    public Observable<String> updatePayPwd(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.updatePayPwd(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<String>>() {
                    @Override
                    public Observable<String> call(Pair<MetaBean, String> pair) {
                        return toObservable(pair);
                    }
                });
    }

    /**
     * 修改登录手机号
     * @param reqData 请求参数
     */
    public Observable<String> modifyUserName(JSONObject reqData) {
        RequestMap requestMap = new RequestMap();
        requestMap.put(CommonConfig.REQDATA, new Gson().toJson(reqData));
        return callMapAfterLogin(service.modifyUserName(requestMap.getReqParams(true)))
                .flatMap(new Func1<Pair<MetaBean, String>, Observable<String>>() {
                    @Override
                    public Observable<String> call(Pair<MetaBean, String> pair) {
                        return toObservable(pair);
                    }
                });
    }
}
